import httpService, { RequestParam } from './request';
import http from '@ohos.net.http';

export class PCconnectManager {
  requestOptions = {}

  InitParam(jstr) {
    this.requestOptions = {
      method: http.RequestMethod.POST,
      header: [{
        'Content-Type': 'application/json; charset=UTF-8',
        'Accept': 'application/json'
      }],
      extraData: jstr
    }
  }

  async parseResult(data){
    let TAG = "_:_parseResult"
    console.log(TAG, "origin", data.replace(/\n/g,""))
    console.log(TAG,"parsed",JSON.stringify(JSON.parse(data)))
    return data.replace(/\n/g,"")
  }

  async connect(ip) {
    let TAG = "_:_PCconnect"
    ip="http://"+ip+":8080"
    console.log(TAG,"entered with ip",ip)
    this.InitParam(JSON.stringify({ "content": "hello" }))
    console.log(TAG,"param inited",JSON.stringify(this.requestOptions))
    let result:string = await httpService.requestMain(ip, this.requestOptions, (data) => {
      console.log(TAG,"received data",data.replace(/\n/g,""))
      return this.parseResult(data)
    })
    console.log(TAG, "result", result)
    return result
  }
}